package com.fyx.timedTask;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyx.config.SecondaryMongoConfig;
import com.fyx.entity.*;
import com.fyx.mapper.ChannelLtvMapper;
import com.fyx.mapper.MeituanMaiInfoMapper;
import com.fyx.mapper.MeituanReStayMapper;
import com.fyx.mapper.SysDictDetailMapper;
import com.fyx.until.DateUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ltv留存
 */
@Slf4j
@Component(value="ChannelLtvTask")
public class ChannelLtvTask {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ChannelLtvMapper channelLtvMapper;

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;

    @Resource(name = SecondaryMongoConfig.MONGO_TEMPLATE)
    private MongoTemplate secondaryMongoTemplate;

    @Autowired
    private MeituanReStayMapper meituanReStayMapper;

    @Autowired
    private MeituanMaiInfoMapper meituanMaiInfoMapper;

    public void run(){
        log.info("ltv留存：开始任务=======================");
        long time = DateUtil.getDayBegin().getTime() / 1000;

        long nowTime=new Date().getTime()/1000;
        if(time==nowTime){
            time=DateUtil.getBeginDayOfYesterday().getTime()/1000;
        }
        long endTime =time+3600*24;

        meituan(time,endTime);
        meituanMai(time,endTime);
        record(time);
        log.info("ltv留存：执行完毕=======================");
    }

    public void record(long time) {
        QueryWrapper<SysDictDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_id",16);
        List<SysDictDetail> channelList =sysDictDetailMapper.selectList(wrapper);


        List<ChannelLtv> channelLtvList = new ArrayList<>();
        //今日
        Criteria criteria = new Criteria();
        criteria.andOperator(Criteria.where("day").gte(time).lt(time + 24 * 3600));
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
                Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
        );
        AggregationResults<ChannelDataEntity> ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
        List<ChannelDataEntity> ltvList = ar.getMappedResults();
        for (int i = 0; i < channelList.size(); i++) {
            ChannelLtv channelLtv = new ChannelLtv();
            String channel = channelList.get(i).getValue();
            channelLtv.setTime(time);
            channelLtv.setChannel(channel);
            if (ltvList.size() > 0) {
                for (int j = 0; j < ltvList.size(); j++) {
                    String channel1 = ltvList.get(j).getChannel();
                    if (channel1 != null && channel.equals(channel1)) {
                        if (ltvList.get(j).getLtv() != null) {
                            channelLtv.setTotalCharge(ltvList.get(j).getLtv()[0]);
                            channelLtv.setRegNum(ltvList.get(j).getReg_num());
                        }
                    }
                }
            }

            channelLtvList.add(channelLtv);
        }

        QueryWrapper<ChannelLtv> wrapper1=new QueryWrapper<>();
        wrapper1.eq("time",time);
        channelLtvMapper.delete(wrapper1);
        channelLtvMapper.insertList(channelLtvList);
//
//        //前1天
//        List<ChannelLtv> channelLtvList1 = new ArrayList<>();
//        criteria = new Criteria();
//        criteria.andOperator(Criteria.where("day").gte(time - 24 * 3600).lt(time));
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
//                Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
//        );
//        ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
//        List<ChannelDataEntity> ltvList1 = ar.getMappedResults();
//
//        for (int i = 0; i < channelList.size(); i++) {
//            ChannelLtv channelLtv = new ChannelLtv();
//            String channel = channelList.get(i).getValue();
//            channelLtv.setTime(time - 24 * 3600);
//            channelLtv.setChannel(channel);
//            if (ltvList1.size() > 0) {
//                for (int j = 0; j < ltvList1.size(); j++) {
//                    String channel1 = ltvList1.get(j).getChannel();
//                    if (channel1 != null && channel.equals(channel1)) {
//                        if (ltvList1.get(j).getLtv() != null) {
//                            channelLtv.setTotalCharge(ltvList1.get(j).getLtv()[0]);
//                            channelLtv.setTotalCharge1(ltvList1.get(j).getLtv()[0] + ltvList1.get(j).getLtv()[1]);
//                            channelLtv.setRegNum(ltvList1.get(j).getReg_num());
//                        }
//                    }
//                }
//            }
//
//            channelLtvList1.add(channelLtv);
//        }
//
//        wrapper1=new QueryWrapper<>();
//        wrapper1.eq("time",time - 24 * 3600);
//        channelLtvMapper.delete(wrapper1);
//        channelLtvMapper.insertList(channelLtvList1);
//
//        //前3天
//        List<ChannelLtv> channelLtvList3 = new ArrayList<>();
//        criteria = new Criteria();
//        criteria.andOperator(Criteria.where("day").gte(time - 3 * 24 * 3600).lt(time-2*24*3600));
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
//                Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
//        );
//        ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
//        List<ChannelDataEntity> ltvList3 = ar.getMappedResults();
//
//        for (int i = 0; i < channelList.size(); i++) {
//            ChannelLtv channelLtv = new ChannelLtv();
//            String channel = channelList.get(i).getValue();
//            channelLtv.setTime(time - 3 * 24 * 3600);
//            channelLtv.setChannel(channel);
//            if (ltvList3.size() > 0) {
//                for (int j = 0; j < ltvList3.size(); j++) {
//                    String channel1 = ltvList3.get(j).getChannel();
//                    if (channel1 != null && channel.equals(channel1)) {
//                        if (ltvList3.get(j).getLtv() != null) {
//                            channelLtv.setTotalCharge(ltvList3.get(j).getLtv()[0]);
//                            channelLtv.setTotalCharge1(ltvList3.get(j).getLtv()[0] + ltvList3.get(j).getLtv()[1]);
//                            channelLtv.setTotalCharge3(ltvList3.get(j).getLtv()[0] + ltvList3.get(j).getLtv()[1] + ltvList3.get(j).getLtv()[2]);
//                            channelLtv.setRegNum(ltvList3.get(j).getReg_num());
//                        }
//                    }
//                }
//            }
//
//            channelLtvList3.add(channelLtv);
//        }
//
//        wrapper1=new QueryWrapper<>();
//        wrapper1.eq("time",time - 3 * 24 * 3600);
//        channelLtvMapper.delete(wrapper1);
//        channelLtvMapper.insertList(channelLtvList3);
//
//        //前7天
//        List<ChannelLtv> channelLtvList7 = new ArrayList<>();
//        criteria = new Criteria();
//        criteria.andOperator(Criteria.where("day").gte(time - 7 * 24 * 3600).lt(time-6*24*3600));
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
//                Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
//        );
//        ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
//        List<ChannelDataEntity> ltvList7 = ar.getMappedResults();
//        for (int i = 0; i < channelList.size(); i++) {
//            ChannelLtv channelLtv = new ChannelLtv();
//            String channel = channelList.get(i).getValue();
//            channelLtv.setTime(time - 7 * 24 * 3600);
//            channelLtv.setChannel(channel);
//            if (ltvList7.size() > 0) {
//                for (int j = 0; j < ltvList7.size(); j++) {
//                    String channel1 = ltvList7.get(j).getChannel();
//                    if (channel1 != null && channel.equals(channel1)) {
//                        if (ltvList7.get(j).getLtv() != null) {
//                            channelLtv.setTotalCharge(ltvList7.get(j).getLtv()[0]);
//                            channelLtv.setTotalCharge1(ltvList7.get(j).getLtv()[0] + ltvList7.get(j).getLtv()[1]);
//                            channelLtv.setTotalCharge3(ltvList7.get(j).getLtv()[0] + ltvList7.get(j).getLtv()[1] + ltvList7.get(j).getLtv()[2]);
//                            channelLtv.setTotalCharge7(ltvList7.get(j).getLtv()[0] + ltvList7.get(j).getLtv()[1] + ltvList7.get(j).getLtv()[2] + ltvList7.get(j).getLtv()[3] + ltvList7.get(j).getLtv()[4] + ltvList7.get(j).getLtv()[5] + ltvList7.get(j).getLtv()[6]);
//                            channelLtv.setRegNum(ltvList7.get(j).getReg_num());
//                        }
//                    }
//                }
//            }
//            channelLtvList7.add(channelLtv);
//        }
//
//        wrapper1=new QueryWrapper<>();
//        wrapper1.eq("time",time - 7 * 24 * 3600);
//        channelLtvMapper.delete(wrapper1);
//        channelLtvMapper.insertList(channelLtvList7);
//
//        //前14天
//        List<ChannelLtv> channelLtvList14 = new ArrayList<>();
//        criteria = new Criteria();
//        criteria.andOperator(Criteria.where("day").gte(time - 14 * 24 * 3600).lt(time-13*24*3600));
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
//                Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
//        );
//        ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
//        List<ChannelDataEntity> ltvList14 = ar.getMappedResults();
//        for (int i = 0; i < channelList.size(); i++) {
//            ChannelLtv channelLtv = new ChannelLtv();
//            String channel = channelList.get(i).getValue();
//            channelLtv.setTime(time - 14 * 24 * 3600);
//            channelLtv.setChannel(channel);
//            if (ltvList14.size() > 0) {
//                for (int j = 0; j < ltvList14.size(); j++) {
//                    String channel1 = ltvList14.get(j).getChannel();
//                    if (channel1 != null && channel.equals(channel1)) {
//                        if (ltvList14.get(j).getLtv() != null) {
//                            channelLtv.setTotalCharge(ltvList14.get(j).getLtv()[0]);
//                            channelLtv.setTotalCharge1(ltvList14.get(j).getLtv()[0] + ltvList14.get(j).getLtv()[1]);
//                            channelLtv.setTotalCharge3(ltvList14.get(j).getLtv()[0] + ltvList14.get(j).getLtv()[1] + ltvList14.get(j).getLtv()[2]);
//                            channelLtv.setTotalCharge7(ltvList14.get(j).getLtv()[0] + ltvList14.get(j).getLtv()[1] + ltvList14.get(j).getLtv()[2] + ltvList14.get(j).getLtv()[3] + ltvList14.get(j).getLtv()[4] + ltvList14.get(j).getLtv()[5] + ltvList14.get(j).getLtv()[6]);
//                            channelLtv.setTotalCharge14(ltvList14.get(j).getLtv()[0] + ltvList14.get(j).getLtv()[1] + ltvList14.get(j).getLtv()[2] + ltvList14.get(j).getLtv()[3] + ltvList14.get(j).getLtv()[4] + ltvList14.get(j).getLtv()[5] + ltvList14.get(j).getLtv()[6] + ltvList14.get(j).getLtv()[7] + ltvList14.get(j).getLtv()[8] + ltvList14.get(j).getLtv()[9] + ltvList14.get(j).getLtv()[10] + ltvList14.get(j).getLtv()[11] + ltvList14.get(j).getLtv()[12] + ltvList14.get(j).getLtv()[13]);
//                            channelLtv.setRegNum(ltvList14.get(j).getReg_num());
//                        }
//                    }
//                }
//            }
//            channelLtvList14.add(channelLtv);
//
//        }
//
//        wrapper1=new QueryWrapper<>();
//        wrapper1.eq("time",time - 14 * 24 * 3600);
//        channelLtvMapper.delete(wrapper1);
//        channelLtvMapper.insertList(channelLtvList14);

        //前30
        Integer[] days={1,2,3,4,5,6,7,14,30,60,90,120};//,150,180,210,240,270,300,330,360};
        for (int d=0;d<days.length;d++){
            List<ChannelLtv> channelLtvList30 = new ArrayList<>();
            criteria = new Criteria();
            criteria.andOperator(Criteria.where("day").gte(time - days[d] * 24 * 3600).lt(time-(days[d]-1)*24*3600));
            aggregation = Aggregation.newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
                    Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
            );
            ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
            List<ChannelDataEntity> ltvList30 = ar.getMappedResults();
            for (int i = 0; i < channelList.size(); i++) {
                Map channelLtv = new HashMap();
                String channel = channelList.get(i).getValue();
                channelLtv.put("time",time - days[d] * 24 * 3600);
                channelLtv.put("channel",channel);
                if (ltvList30.size() > 0) {

                    for (int j = 0; j < ltvList30.size(); j++) {
                        String channel1 = ltvList30.get(j).getChannel();
                        if (channel1 != null && channel.equals(channel1)&&ltvList30.get(j).getLtv() != null) {
                            channelLtv.put("totalCharge", ltvList30.get(j).getLtv()[0]);
                            Integer result=0;
                            for(int l=0;l<(d+1);l++) {
                                for (int k = 0; k < (days[l]+1); k++) {
                                    result += ltvList30.get(j).getLtv()[k];
                                }
                                channelLtv.put("totalCharge" + days[l], result);
                                result=0;
                            }
                            channelLtv.put("regNum",ltvList30.get(j).getReg_num());
                        }
                    }
                }

                channelLtvList30.add( JSON.parseObject(JSON.toJSONString(channelLtv), ChannelLtv.class));
            }

            wrapper1=new QueryWrapper<>();
            wrapper1.eq("time",time - days[d] * 24 * 3600);

            channelLtvMapper.delete(wrapper1);
            channelLtvMapper.insertList(channelLtvList30);
        }
//        List<ChannelLtv> channelLtvList30 = new ArrayList<>();
//        criteria = new Criteria();
//        criteria.andOperator(Criteria.where("day").gte(time - 30 * 24 * 3600).lt(time-29*24*3600));
//        aggregation = Aggregation.newAggregation(
//                Aggregation.match(criteria),
//                Aggregation.group("channel").first("ltv").as("ltv").first("reg_num").as("reg_num"),
//                Aggregation.project("channel", "ltv","reg_num").and("channel").previousOperation()
//        );
//        ar = mongoTemplate.aggregate(aggregation, "channel_data", ChannelDataEntity.class);
//        List<ChannelDataEntity> ltvList30 = ar.getMappedResults();
//        for (int i = 0; i < channelList.size(); i++) {
//            ChannelLtv channelLtv = new ChannelLtv();
//            String channel = channelList.get(i).getValue();
//            channelLtv.setTime(time - 30 * 24 * 3600);
//            channelLtv.setChannel(channel);
//            if (ltvList30.size() > 0) {
//                for (int j = 0; j < ltvList30.size(); j++) {
//                    String channel1 = ltvList30.get(j).getChannel();
//                    if (channel1 != null && channel.equals(channel1)) {
//                        if (ltvList30.get(j).getLtv() != null) {
//                            channelLtv.setTotalCharge(ltvList30.get(j).getLtv()[0]);
//                            channelLtv.setTotalCharge1(ltvList30.get(j).getLtv()[0] + ltvList30.get(j).getLtv()[1]);
//                            channelLtv.setTotalCharge3(ltvList30.get(j).getLtv()[0] + ltvList30.get(j).getLtv()[1] + ltvList30.get(j).getLtv()[2]);
//                            channelLtv.setTotalCharge7(ltvList30.get(j).getLtv()[0] + ltvList30.get(j).getLtv()[1] + ltvList30.get(j).getLtv()[2] + ltvList30.get(j).getLtv()[3] + ltvList30.get(j).getLtv()[4] + ltvList30.get(j).getLtv()[5] + ltvList30.get(j).getLtv()[6]);
//                            channelLtv.setTotalCharge14(ltvList30.get(j).getLtv()[0] + ltvList30.get(j).getLtv()[1] + ltvList30.get(j).getLtv()[2] + ltvList30.get(j).getLtv()[3] + ltvList30.get(j).getLtv()[4] + ltvList30.get(j).getLtv()[5] + ltvList30.get(j).getLtv()[6] + ltvList30.get(j).getLtv()[7] + ltvList30.get(j).getLtv()[8] + ltvList30.get(j).getLtv()[9] + ltvList30.get(j).getLtv()[10] + ltvList30.get(j).getLtv()[11] + ltvList30.get(j).getLtv()[12] + ltvList30.get(j).getLtv()[13]);
//                            channelLtv.setTotalCharge30(ltvList30.get(j).getLtv()[0] + ltvList30.get(j).getLtv()[1] + ltvList30.get(j).getLtv()[2] + ltvList30.get(j).getLtv()[3] + ltvList30.get(j).getLtv()[4] + ltvList30.get(j).getLtv()[5] + ltvList30.get(j).getLtv()[6] + ltvList30.get(j).getLtv()[7] + ltvList30.get(j).getLtv()[8] + ltvList30.get(j).getLtv()[9] + ltvList30.get(j).getLtv()[10] + ltvList30.get(j).getLtv()[11] + ltvList30.get(j).getLtv()[12] + ltvList30.get(j).getLtv()[13] + ltvList30.get(j).getLtv()[14] + ltvList30.get(j).getLtv()[15] + ltvList30.get(j).getLtv()[16] + ltvList30.get(j).getLtv()[17] + ltvList30.get(j).getLtv()[18] + ltvList30.get(j).getLtv()[19] + ltvList30.get(j).getLtv()[20] + ltvList30.get(j).getLtv()[21] + ltvList30.get(j).getLtv()[22] + ltvList30.get(j).getLtv()[23] + ltvList30.get(j).getLtv()[24] + ltvList30.get(j).getLtv()[25] + ltvList30.get(j).getLtv()[26] + ltvList30.get(j).getLtv()[27] + ltvList30.get(j).getLtv()[28] + ltvList30.get(j).getLtv()[29]);
//                            channelLtv.setRegNum(ltvList30.get(j).getReg_num());
//                        }
//                    }
//                }
//            }
//            channelLtvList30.add(channelLtv);
//        }
//
//        wrapper1=new QueryWrapper<>();
//        wrapper1.eq("time",time - 30 * 24 * 3600);
//        channelLtvMapper.delete(wrapper1);
//        channelLtvMapper.insertList(channelLtvList30);

    }



    public  void  meituanMai(long time,long endTime){
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
        String dateStr = ft.format(new Date()).replace("-", "").trim();
        String tableName="collect_data_"+dateStr;
        MeituanMaiInfo meituanMaiInfo = new MeituanMaiInfo();
        meituanMaiInfo.setTime((int) time);

         //商城钻石广告
                Criteria criteria = new Criteria();
                criteria.and("time").gte(time).lte(endTime);
                criteria.and("evid").is(1000000123);
                criteria.and("channel").is("h5hzmjxl_meituan_re");
                Aggregation aggregation = Aggregation.newAggregation(
                        Aggregation.match(criteria),
                        Aggregation.group("numid").count().as("v1"),
                        Aggregation.group("numid").count().as("v1")
                );
                AggregationResults<StaticeEntity> ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
                List<StaticeEntity> list = ar.getMappedResults();
                if (list.size() > 0) {
                    meituanMaiInfo.setPhoneBill(list.get(0).getV1());
                }

        criteria = new Criteria();
        criteria.and("time").gte(time).lte(endTime);
        criteria.and("evid").is(1000000124);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("numid").count().as("v1"),
                Aggregation.group("numid").count().as("v1")
        );
        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        list = ar.getMappedResults();
        if (list.size() > 0) {
            meituanMaiInfo.setChangeThree(list.get(0).getV1());
        }

        criteria = new Criteria();
        criteria.and("time").gte(time).lte(endTime);
        criteria.and("evid").is(1000000125);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("numid").count().as("v1"),
                Aggregation.group("numid").count().as("v1")
        );
        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        list = ar.getMappedResults();
        if (list.size() > 0) {
            meituanMaiInfo.setDingQue(list.get(0).getV1());
        }

        criteria = new Criteria();
        criteria.and("time").gte(time).lte(endTime);
        criteria.and("evid").is(1000000126);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("numid").count().as("v1"),
                Aggregation.group("numid").count().as("v1")
        );
        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        list = ar.getMappedResults();
        if (list.size() > 0) {
            meituanMaiInfo.setOpenRedPacket(list.get(0).getV1());
        }

        criteria = new Criteria();
        criteria.and("time").gte(time).lte(endTime);
        criteria.and("evid").is(1000000127);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("numid").count().as("v1"),
                Aggregation.group("numid").count().as("v1")
        );
        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        list = ar.getMappedResults();
        if (list.size() > 0) {
            meituanMaiInfo.setNextGame(list.get(0).getV1());
        }

        criteria = new Criteria();
        criteria.and("time").gte(time).lte(endTime);
        criteria.and("evid").is(1000000128);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("numid").count().as("v1"),
                Aggregation.group("numid").count().as("v1")
        );
        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        list = ar.getMappedResults();
        if (list.size() > 0) {
            meituanMaiInfo.setBack(list.get(0).getV1());
        }

        criteria = new Criteria();
        criteria.and("time").gte(time).lte(endTime);
        criteria.and("evid").is(1000000129);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("numid").count().as("v1"),
                Aggregation.group("numid").count().as("v1")
        );
        ar = secondaryMongoTemplate.aggregate(aggregation, tableName, StaticeEntity.class);
        list = ar.getMappedResults();
        if (list.size() > 0) {
            meituanMaiInfo.setStart(list.get(0).getV1());
        }
        QueryWrapper<MeituanMaiInfo> wrapper =new QueryWrapper<>();
        wrapper.eq("time",time);
        MeituanMaiInfo meituanMaiInfo1=meituanMaiInfoMapper.selectOne(wrapper);
        if(meituanMaiInfo1==null) {
            meituanMaiInfoMapper.insert(meituanMaiInfo);
        }else {
            meituanMaiInfoMapper.update(meituanMaiInfo,wrapper);
        }



    }

    public  void  meituan(long time,long endTime){

        String gameName="login_game_rec_";
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");
        String dateStr = ft.format(new Date()).replace("-", "").trim();
        gameName = gameName + dateStr;
        Criteria criteria = new Criteria();
        criteria.and("unix_time").gte(time).lte(endTime);
        criteria.and("gameType").is(501);
        criteria.and("channel").is("h5hzmjxl_meituan_re");
        criteria.and("taskUser.unix_time").gte(time).lte(endTime);
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.lookup("register_rec_"+dateStr,"id","id","taskUser"),
                Aggregation.match(criteria),
                Aggregation.group("id").count().as("num"),
                Aggregation.group("num").count().as("v1").first("num").as("v2")
        );
        AggregationResults<StaticeEntity>  ar = secondaryMongoTemplate.aggregate(aggregation, gameName, StaticeEntity.class);
        List<StaticeEntity> list = ar.getMappedResults();
        System.out.println(list);
        MeituanReStay meituanReStay=new MeituanReStay();
        meituanReStay.setTime((int)time);
        if(list.size()>0){
            for (StaticeEntity staticeEntity:list){
                if(staticeEntity.getV2()==1){
                    meituanReStay.setPerson1(staticeEntity.getV1());
                }
                else if(staticeEntity.getV2()==2){
                    meituanReStay.setPerson2(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==3){
                    meituanReStay.setPerson3(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==4){
                    meituanReStay.setPerson4(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==5){
                    meituanReStay.setPerson5(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==6){
                    meituanReStay.setPerson6(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==7){
                    meituanReStay.setPerson7(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==8){
                    meituanReStay.setPerson8(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==9){
                    meituanReStay.setPerson9(staticeEntity.getV1());
                } else if(staticeEntity.getV2()==10){
                    meituanReStay.setPerson10(staticeEntity.getV1());
                }
            }
        }
        QueryWrapper<MeituanReStay> wrapper=new QueryWrapper<>();
        wrapper.eq("time",time);
       MeituanReStay meituanReStay1= meituanReStayMapper.selectOne(wrapper);
       if(meituanReStay1==null){
           meituanReStayMapper.insert(meituanReStay);
       }else {
           meituanReStayMapper.update(meituanReStay,wrapper);
       }
    }

}
